<?php
//无限分类
require 'database.php';
$base=new Db;


$cate_arr=$base->doSql('select * from hkcms_auth_group order by id asc');


function getTree($cate_arr=[],$pid=0,$level=0){  
    static $cate_tree_arr=[];
    for ($i = 0; $i < count($cate_arr); $i++) {
        
        if($cate_arr[$i]["parent_id"]==$pid){
            $cate_arr[$i]["level"]=$level;
            $cate_tree_arr[]=$cate_arr[$i];
            getTree($cate_arr,$cate_arr[$i]['id'],$level+1);
        }
        
    }
    return $cate_tree_arr;
}
$cate_tree_arr=getTree($cate_arr);
$spacer="&nbsp;&nbsp;";
for ($i = 0; $i < count($cate_tree_arr); $i++) {
    $arr=[];
    // echo str_repeat($spacer,$cate_tree_arr[$i]['level']).$cate_tree_arr[$i]['name'].'<br>';    
    
    $arr=$base->doSql('select * from hkcms_auth_group where parent_id="'.$cate_tree_arr[$i]["id"].'" order by id asc');
    if($arr){
        $cate_tree_arr[$i]["haschild"]=1;
    }else{
        $cate_tree_arr[$i]["haschild"]=0;
    }
    
    $str=str_repeat($spacer,$cate_tree_arr[$i]['level']);
    
    $raw_name="";
    
    if($str){
        $cate_tree_arr[$i]["spacer"]=str_repeat($spacer,$cate_tree_arr[$i]['level'])."├";
        
       
        $raw_name=$cate_tree_arr[$i]["name"];
        $cate_tree_arr[$i]["raw_name"]=$raw_name;
        
        $cate_tree_arr[$i]["name"]=$cate_tree_arr[$i]["spacer"].$cate_tree_arr[$i]["name"];
        
    }else{
        $cate_tree_arr[$i]["spacer"]="";
    }

    
    
    
    $cate_tree_arr[$i]["create_time"]=date('Y-m-d H:i:s',$cate_tree_arr[$i]["create_time"]);
    $cate_tree_arr[$i]["update_time"]=date('Y-m-d H:i:s',$cate_tree_arr[$i]["update_time"]);
    
    
  
    
    
}



$data=[
    "total" =>count($cate_arr), 
    "rows"  =>$cate_tree_arr
];

return json($data);

?>



